package com.zx.flow.db;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.zx.flow.api.config.FlowConfig;
import com.zx.flow.api.exception.FlowException;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.model.FlowNode;
import com.zx.flow.api.execute.request.manager.ManagerFlowConfigListForm;
import com.zx.flow.api.execute.request.manager.ManagerFlowListForm;
import com.zx.flow.api.util.AssertUtil;
import com.zx.flow.api.util.FlowConfigUtil;
import com.zx.flow.db.entity.FlowSecretEntity;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

@Slf4j
public class FlowStorageMockHandler implements FlowStorageHandler {

    /**
     * 模拟存储数据
     */
    private Map<String, FlowContext> dataMock = new HashMap<>();


    @Override
    public List<FlowSecretEntity> getSecret() {
        FlowSecretEntity secret = new FlowSecretEntity();
        secret.setSecretId("test-key");
        secret.setSecretKey("test-value");
        return Lists.newArrayList(secret);
    }

    @Override
    public IPage<FlowContext> getFlowList(ManagerFlowListForm form) {
        return null;
    }

    @Override
    public IPage<FlowConfig> getFlowConfigList(ManagerFlowConfigListForm form) {
        return null;
    }

    @Override
    public List<FlowConfig> getAllFlowConfig() {
        return FlowConfigUtil.loadLocalConfig();
    }

    @Override
    public void saveFlowConfig(FlowConfig flowConfig) {
        AssertUtil.throwFE("mock 模式下，不支持 saveFlowConfig");
    }

    @Override
    public void saveFlow(FlowContext flowContext) {
        flowContext.setUpdateTime(new Date());
        this.dataMock.put(flowContext.getId(), flowContext);
    }

    @Override
    public FlowContext getFlow(String flowId) {
        return dataMock.get(flowId);
    }

    @Override
    public void saveNode(String flowId, FlowNode flowNode) {
        FlowContext flow = getFlow(flowId);
        if (flow == null) {
            throw new FlowException("流程不存在, flowId=" + flowId);
        }
        FlowNode node = getNode(flowId, flowNode.getId());
        if (node == null) {
            flow.getNodes().add(flowNode);
        } else {
            BeanUtils.copyProperties(flowNode, node);
        }
    }

    @Override
    public List<FlowNode> getNodes(String flowId) {
        FlowContext flow = this.getFlow(flowId);
        List<FlowNode> nodes = flow.getNodes();
        if (CollectionUtils.isEmpty(nodes)) {
            nodes = new ArrayList<>();
            flow.setNodes(nodes);
        }
        return nodes;
    }

    @Override
    public FlowNode getNode(String flowId, String nodeId) {
        for (FlowNode node : getNodes(flowId)) {
            if (Objects.equals(node.getId(), nodeId)) {
                return node;
            }
        }
        return null;
    }

}
